package com.itheima.tlias84.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias84.dto.SelectAllEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDatailDTO;
import com.itheima.tlias84.entity.Expr;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.mapper.ExprMapper;
import com.itheima.tlias84.po.InsertEmpParam;
import com.itheima.tlias84.po.SelectEmpPageParam;
import com.itheima.tlias84.po.UpdateEmpParam;
import com.itheima.tlias84.service.EmpService;
import com.itheima.tlias84.utils.ResultUtil;
import com.itheima.tlias84.vo.PageVO;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl  implements EmpService {
    private static final Logger log = LoggerFactory.getLogger(EmpServiceImpl.class);
    @Resource
    EmpMapper empMapper;
    @Resource
    ExprMapper exprMapper;
    //分页查询
    @Override
    public ResultUtil selectEmpByPage(SelectEmpPageParam param) {
//        param.setPage((param.getPage()-1)* param.getPageSize());
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectEmpDTO> dtos = empMapper.selectEmpByPage(param);
        PageVO<SelectEmpDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(dtos);
        return  ResultUtil.success(pageVO);
    }

    @Override
    public int deleteEmpByIds(List<Integer> ids) {

        int flag = empMapper.deleteEmpByIds(ids);
        return flag;
    }

    @Override
    public int insertEmp( InsertEmpParam param) {
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());

        //插入工作履历
        log.info("员工回显的主键{}", param.getId());
        List<Expr> exprList = param.getExprList();
        if (CollectionUtils.isNotEmpty(exprList)) {
            //赋值员工id 以及时间
            exprList = exprList.stream().map(t -> {
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i = exprMapper.batchInsertExpr(exprList);
            log.info("插入员工履历{}", i);
        }

//        int flag = empMapper.insertEmp(param);
//        log.info(param.toString());
        return empMapper.insertEmp(param);
    }

//    @Override
//    public ResultUtil selectEmpByPage(SelectEmpPageParam page) {
//        //计算分页
//        //1  --5   0   --5
//        //2  --5   (page-1)*pageSize ,   pageSize
//        //3  --5   2*5   --5
//        //4  --5   3*5   --5
//
//
////        page.setPage((page.getPage()-1)* page.getPageSize());
//        //注意 pageHelper会自动执行一个count sql ，并且会自动拼接limit
//        //因此必须写在自己的sql之前
//        Page<Object> page1 = PageHelper.startPage(page.getPage(), page.getPageSize());
//        List<SelectEmpDTO> dtos = empMapper.selectEmpByPage(page);
//        PageVO<SelectEmpDTO> pageVO = new PageVO< >();
//        pageVO.setTotal(page1.getTotal());
//        pageVO.setRows(dtos);
//        return ResultUtil.success(pageVO);
//    }

    @Override
    public SelectEmpDatailDTO selectEmpById(Integer id) {
        return empMapper.selectEmpById(id);
    }

    @Override
    public int updateEmp(UpdateEmpParam updateEmpParam) {
        updateEmpParam.setUpdateTime(LocalDateTime.now());
        return empMapper.updateEmp(updateEmpParam);
    }

    @Override
    public List<SelectAllEmpDTO> selectAllEmp() {
        List<SelectAllEmpDTO> dtos = empMapper.selectAllEmp();
        return dtos;
    }

    @Override
    public ResultUtil countEmpByJob() {
        List<Integer> jobs = empMapper.countEmpByJob();
        //统计每种职位有多少员工
        Map<String, List<Integer>> map = jobs.stream().collect(Collectors.groupingBy(t -> {
            return switch (t) {
                case 1 -> "班主任";
                case 2 -> "讲师";
                case 3 -> "学工主管";
                case 4 -> "教研主管";
                case 5 -> "咨询部";
                default -> "其他";
            };
        }));
        Set<String> jobList = map.keySet();
        List<Integer> dataList = map.values().stream().map((t) -> t.size()).toList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("jobList",jobList);
        jsonObject.put("dataList",dataList);
        return ResultUtil.success(jsonObject);
    }

}



